package com.house.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.house.common.BaseContext;
import com.house.entity.Building;
import com.house.mapper.BuildingMapper;
import com.house.service.BuildingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class BuildingServiceImpl implements BuildingService {

    @Autowired
    private BuildingMapper buildingMapper;

    @Override
    public int save(Building building) {
        building.setUserId(BaseContext.getThreadUserId());
        return buildingMapper.insert(building);
    }

    @Override
    public Building selectBuildingById(Long id) {
        return buildingMapper.selectByBuildingId(id);
    }

    @Override
    public void updateBuildingById(Building building) {
        LambdaQueryWrapper<Building> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Building::getBuildingId, building.getBuildingId());
        buildingMapper.update(building, queryWrapper);
    }

    @Override
    public Page<Building> selectPage(int page, int pageSize, int type, long startAreaId, long endAreaId, String buildingName) {
        Page<Building> pageInfo= new Page<>(page, pageSize);
        LambdaQueryWrapper<Building> queryWrapper = new LambdaQueryWrapper<>();
        if (type == 1) {
            queryWrapper.eq(Building::getUserId ,BaseContext.getThreadUserId());
        } else if (type == 2) {
            queryWrapper.between(Building::getAreaId, startAreaId, endAreaId);
        }
        queryWrapper.like(buildingName != null, Building::getBuildingName, buildingName);
        queryWrapper.orderByDesc(Building::getCreateTime);
        return buildingMapper.selectPage(pageInfo, queryWrapper);
    }

    @Override
    public int delete(Long buildingId) {
        return buildingMapper.delete(buildingId);
    }
}
